//
//  CheckVersionViewModel.m
//  mobile
//
//  Created by li on 2017/10/18.
//  Copyright © 2017年 li. All rights reserved.
//

#import "CheckVersionViewModel.h"
#import <AFNetworking.h>
@implementation CheckVersionViewModel

+ (void)getVersionInfoComplete:(versionCallback)handle;{
    
       AFHTTPSessionManager  *manager = [[AFHTTPSessionManager alloc] init];
            
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", @"application/json", @"text/json", @"text/javascript",@"application/x-www-form-urlencoded", nil];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
            // 不设置会报-1016或者会有编码问题
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    [manager GET:[AppConfig versionURL] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if ([responseObject[@"errorCode"] integerValue] == 0) {
            
            VersionModel *data = [[VersionModel alloc] initWithDictionary:responseObject[@"data"]];
            
            handle(YES,nil,data);
        }else{
            handle(NO,responseObject[@"message"],nil);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        handle(NO,error.userInfo[@"message"],nil);
    }];
}

+ (void)getWebVersion{
    NSString *url = @"https://tomato.harsonserver.com/public/app/36a80b0c-4eaf-4bb0-91ce-51572e123207/tag";
    
    NSURLSession *session = [NSURLSession sessionWithConfiguration: [NSURLSessionConfiguration defaultSessionConfiguration] delegate: nil delegateQueue: [NSOperationQueue new]];
    [[session dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
        if (error || httpResponse.statusCode != 200 || !data) {
            NSLog(@"->>> 请求H5版本信息失败:%@<<<-",error);
            return;
        }
        
        NSError *err;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&err];
        if ([dict[@"status"] isEqualToString:@"success"]) {
            [[NSUserDefaults standardUserDefaults] setObject:dict[@"data"][@"sec"] forKey:@"releaseVersion"];
            [[NSUserDefaults standardUserDefaults] setObject:dict[@"data"][@"sit"] forKey:@"debugVersion"];
        }       
    }] resume] ;
}


@end
